package com.kmxd.ams.app.watermark;

import cn.hutool.core.io.FileUtil;
import com.kmxd.ams.client.archive.IArchiveWatermarkSvc;
import com.kmxd.ams.client.archive.vo.ArchiveWatermarkVO;
import com.kmxd.ams.client.watermark.IWatermarkSvc;
import com.kmxd.ams.client.watermark.vo.WatermarkVO;
import com.kmxd.ams.core.common.constant.Constants;
import com.kmxd.ams.core.common.enums.ArchWatermarkTypeEnum;
import com.kmxd.ams.core.oss.template.OssTemplate;
import com.kmxd.ams.core.util.WatermarkUtil;
import java.awt.*;
import java.io.File;
import java.io.FileInputStream;
import java.util.Objects;
import javax.swing.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * 添加水印友虹实现
 *
 * @author nickbi
 */
@Slf4j
@Service(Constants.WATERMARK_SVC_PREFIX + "youhong")
@RequiredArgsConstructor
public class WatermarkYouHongSvcImpl implements IWatermarkSvc {

  private final IArchiveWatermarkSvc archiveWatermarkSvc;
  private final OssTemplate ossTemplate;

  @Value("${youhong.host}")
  private String host;

  @Override
  public WatermarkVO addWatermark(Long archTypeId, File file, String waterMarkFilePath) {
    try {

      if (Objects.isNull(file)) {
        return WatermarkVO.builder().build();
      }

      ArchiveWatermarkVO watermark = archiveWatermarkSvc.getByArchTypeId(archTypeId);
      if (Objects.isNull(watermark)) {
        return WatermarkVO.builder().build();
      }

      // 目录不存在，创建目录
      if (!ossTemplate.exist(waterMarkFilePath)) {
        new File(waterMarkFilePath);
      }
      int angle = watermark.getAngle().intValue();
      if (watermark.getType().equals(ArchWatermarkTypeEnum.IMAGE.getType())) {
        // 图片信息
        String imagePath = ossTemplate.getAttachmentPath(null) + watermark.getPicPath();
        FileInputStream image = new FileInputStream(imagePath);
        // 图片类型
        String imageType = FileUtil.getSuffix(imagePath);
        // 透明度
        int alpha = watermark.getOpacity().intValue();
        // 是否平铺
        boolean tiled = false;

      } else {
        String text =
            watermark.getType().equals(ArchWatermarkTypeEnum.TEXT.getType())
                ? watermark.getText()
                : WatermarkUtil.format(watermark.getDynamicInfo());
      }

      return WatermarkVO.builder()
          .file(ossTemplate.getFile(waterMarkFilePath))
          .path(waterMarkFilePath)
          .build();
    } catch (Exception e) {
      log.error(e.getMessage(), e);
      return WatermarkVO.builder().build();
    }
  }

  private Integer getWidth(Integer angle, Integer textH, int textW, Integer fontSize) {
    switch (angle) {
      case 0:
        return textW * fontSize;
      case 90:
        return textH;
      case 180:
        return textW * fontSize;
      case 270:
        return textH;
      case 360:
        return textW * fontSize;
      default:
        // 计算公式 cos(angle)*文字长度
        return (int) (Math.cos(angle) * textW * fontSize);
    }
  }

  private Integer getHeight(Integer angle, Integer textH, int textW, Integer fontSize) {
    switch (angle) {
      case 0:
        return textH;
      case 90:
        return textW * fontSize;
      case 180:
        return textH;
      case 270:
        return textW * fontSize;
      case 360:
        return textH;
      default:
        // 计算公式 sin(angle)*文字长度
        if (90 < angle && angle < 180) {
          return (int) (Math.cos(angle) * textW * fontSize);
        }
        return (int) (Math.sin(angle) * textW * fontSize);
    }
  }

  private Integer getX(Integer angle, Integer textH, int textW, Integer fontSize) {
    switch (angle) {
      case 0:
        return textW;
      case 90:
        return 20;
      case 180:
        return textW;
      case 270:
        return 20;
      case 360:
        return textW;
      default:
        if (angle < 90) {
          return (int) (Math.cos(angle) * textW);
        }
        if (180 < angle && angle < 270) {
          return textW;
        }
        return 20;
    }
  }

  private Integer getY(Integer angle, Integer textH, int textW, Integer fontSize) {
    switch (angle) {
      case 0:
        return 20;
      case 90:
        return textW;
      case 180:
        return 20;
      case 270:
        return textW;
      case 360:
        return 20;
      default:
        if (180 < angle && angle < 270) {
          return (int) (Math.cos(angle) * textW);
        }
        if (270 < angle && angle < 360) {
          return textW;
        }
        return (int) (Math.sin(angle) * textW);
    }
  }

  private String getPageFream(File file) {
    return "200 200";
  }
}
